// wjg
// 
//////////////////////////////////////////////////////////
var topicContext = {};

//(function(){

var OBJ_NAME = 'Topic';
var OBJ_LABEL = 'Topic';
var OBJ_ID = 'id';
var FORM_ID = OBJ_NAME+'EditForm';
var FORM_PANEL_ID = OBJ_NAME+'EditPanel';
var URL_PREFIX = URL_ROOT+'/topic';
var URL_PAGE_QUERY = URL_PREFIX+"/list.json";

function addXXX(){
	var title = '新建 '+OBJ_LABEL;
	var form = document.getElementById(FORM_ID);
	FormUtil.clearForm(form);
	
	popup(FORM_PANEL_ID, title, 450);
	return;
}

function editXXX(id){
	var title = (id?"编辑 ":"新建 ")+OBJ_LABEL;
	var url_get = URL_PREFIX+"/get.json";
	var form = document.getElementById(FORM_ID);
	FormUtil.clearForm(form);

	var values = {"id":id};
	var onJsonLoad = function(result){
			popup(FORM_PANEL_ID, title, 450);
			FormUtil.setFormJson(form, result.data);
	};
	ajaxRequest(url_get, values, onJsonLoad);
}

function saveXXX(){
	var form = document.getElementById(FORM_ID);
	var check = function(submitFun){
		if(!_checkXXXForm()){
			return;
		}
		submitFun();
	}
	var submitFun = function(){
		ajaxSubmit(URL_PREFIX+"/save.json", FORM_ID);
	};
	check(submitFun);
}

function _checkXXXForm(){
	var vConf = {};
	//
 	vConf = [
	{"name":"id","format":"Integer","tip":" 格式错误!"}
	];	var v = new Validator(FORM_ID, vConf, true);
	var ok = v.validate();
	if(!ok){
		return false;
	}
	return true;
}

function deleteXXX(id){
	var DEL_URL = URL_PREFIX+"/delete.json";
	var del_submit = function(){
		var values = {"id":id};
		ajaxRequest(DEL_URL, values, function(result){
			location.reload();
		},false,true);
	}
	var msg = '您确定要删除吗？';
	var name = _getXXXItemName(id);
	if(name){
		msg = '您确定要删除<b style="color:red">'+name+'</b>吗？'
	}
	confirm(msg, del_submit);
}

function deleteSelectedXXX(){
	var idseq = FormUtil.getInputValue('checkbox');
	if(!idseq){
		alert('没有选中');
		return;
	}
	var DEL_URL = URL_PREFIX+"/batchDelete.json";
	var del_submit = function(){
		var values = {"idseq":idseq};
		ajaxRequest(DEL_URL, values, function(result){
			location.reload();
		},false,true);
	}
	var msg = '您确定批量删除吗？';
	confirm(msg, del_submit);
}

function enableXXX(id, obj){
	var enabled = obj.value;
	var fun_submit = function(){
		var url = URL_PREFIX+"/enable.json";
		var values = {"id":id,"enabled":enabled};
		ajaxRequest(url, values, function(result){
			location.reload();
		})
	}
	fun_submit();
}

function _getXXXItemId(item){
	var parent = item.parentNode;
	var id = parent.id;
	var ss = id.split('_');
	return ss[1];
}

function _getXXXItemName(id){
	var item = document.getElementById('XXXListItem_'+id);
	var parent = item.parentNode;
	var tds = parent.childNodes;
	var ss = tds[1].innerText;
	return ss;
}



function makeViewList(objList, append){
	var templateId = 'row_template';
	var tplObj = document.getElementById(templateId);
	var tplParent = tplObj.parentNode;
	//clean
	if(!append){
		DomUtil.removeChildren(tplParent, 1);
		tplParent.appendChild(tplObj);
	}
	//make
	var newObj;
	for(var i=0;i<objList.length;i++){
		newObj = tplObj.cloneNode(true);
		newObj.style.display='';
		tplParent.appendChild(newObj);
		setViewItem(objList[i], newObj);
	}
	tplObj.style.display='none';

	FormUtil.associateCheckBox('checkboxAll', 'checkbox');
}

function setViewItem(dataObj, viewObj){
	viewObj.setAttribute('class','row');
	viewObj.setAttribute('id', 'XXXListItem_' + dataObj.id);
	var children = DomUtil.getChildren(viewObj);
	children[0].firstChild.value = dataObj['id'];
	children[1].innerHTML = Widget.getView(dataObj['id'], 'hidden');
	children[2].innerHTML = Widget.getView(dataObj['name'], 'text');
	children[3].firstChild.onclick = function(){open('../chapter/list.xhtml?topicId='+dataObj.id, '_blank')};
	children[4].innerHTML = Widget.getView(dataObj['enabled'], 'switch');
	children[5].innerHTML = Widget.getView(dataObj['created'], 'date');
	children[6].innerHTML = Widget.getView(dataObj['modified'], 'date');
	children[7].firstChild.onclick = function(){editXXX(dataObj.id);};
	children[8].firstChild.onclick = function(){deleteXXX(dataObj.id);};
}

//onload事件
function initXXXAction(){
	
	EventUtil.addEvent(OBJ_NAME+'AddBtn', 'click', addXXX);
	EventUtil.addEvent(OBJ_NAME+'SaveBtn', 'click', saveXXX);
	EventUtil.addEvent(OBJ_NAME+'DelBtn', 'click', deleteSelectedXXX);
	var params = Browser.getHashParamMap();
	pageQuery(URL_PAGE_QUERY, params);
}

setOnload(initXXXAction);

//})();

